package com.halloon.android.adapter;

import java.util.ArrayList;
import org.json.JSONException;

import com.halloon.android.R;
import com.halloon.android.bean.TweetBean;
import com.halloon.android.image.ImageLoader;
import com.halloon.android.util.ContentTransUtil;
import com.halloon.android.util.TimeUtil;
import com.halloon.android.ui.fragment.TabMainPageFragment.MainPageFragmentCallback;
import com.halloon.android.ui.fragment.TabProfileFragment;
import com.halloon.android.ui.fragment.TweetDetailFragment.TweetDetailFragmentCallback;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TweetContentAdapter extends BaseAdapter{
	
	private Context context;
	private LayoutInflater layoutInflater;
	private ArrayList<TweetBean> tweetBeans;
	
	public TweetContentAdapter(Context context, ArrayList<TweetBean> tweetBeans){
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
		this.tweetBeans = tweetBeans;
	}

	@Override
	public int getCount() {
		return tweetBeans.size();
	}

	@Override
	public TweetBean getItem(int position) {
		if(position < tweetBeans.size()){
			return tweetBeans.get(position);
		}else{
			return new TweetBean();
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void removeItem(int position){
		tweetBeans.remove(position);
		notifyDataSetChanged();
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		TweetViewHolder holder;
		TweetBean tweetBean = getItem(position);
		
		if (tweetBean == null) {
			return null;
		}
		
		if(convertView == null){
			holder = new TweetViewHolder();
			
			convertView = layoutInflater.inflate(R.layout.tweet_content, null);
			
			holder.headImage = (ImageView) convertView.findViewById(R.id.tweet_head);
			holder.title = (TextView) convertView.findViewById(R.id.tweet_title);
			holder.isVip = (ImageView) convertView.findViewById(R.id.is_vip);
			holder.timestamp = (TextView) convertView.findViewById(R.id.tweet_timestamp);
			holder.from = (TextView) convertView.findViewById(R.id.tweet_from);
			holder.commentCount = (TextView) convertView.findViewById(R.id.comment_count);
			holder.forwardCount = (TextView) convertView.findViewById(R.id.forward_count);
			holder.tweetContent = (TextView) convertView.findViewById(R.id.tweet_content);
			holder.tweetLocationText = (TextView) convertView.findViewById(R.id.tweet_location_text);
			holder.tweetImage = (ImageView) convertView.findViewById(R.id.tweet_image);
			holder.forwardContent = (TextView) convertView.findViewById(R.id.forward_content);
			holder.forwardLocationText = (TextView) convertView.findViewById(R.id.forward_location_text);
			holder.forwardImage = (ImageView) convertView.findViewById(R.id.forward_image);
			holder.hasImage = (ImageView) convertView.findViewById(R.id.image_icon);
			holder.sourceLayout = (RelativeLayout) convertView.findViewById(R.id.relativeLayout1);
			convertView.setTag(holder);
		}else{
			holder = (TweetViewHolder) convertView.getTag();
		}
		
		OnClickListener tweetListClickListener = new OnClickListener(){
			@Override
			public void onClick(View v){
				try{
					switch(v.getId()){
					case R.id.tweet_head:
						Bundle bundle = new Bundle();
						bundle.putInt("type", TabProfileFragment.OTHER);
						bundle.putString("name", getItem(position).getName());
						((TweetDetailFragmentCallback) context).setupProfileFragment(bundle);
						break;
					case R.id.tweet_image:
						if(getItem(position).getTweetImage() != null &&
						   getItem(position).getTweetImage().length() > 0) ((MainPageFragmentCallback) context).setupPictureDialog(getItem(position).getTweetImage().getString(0), "/2000");
						if(getItem(position).getVideoImage() != null){
							Intent intent = new Intent();
							intent.setAction("android.intent.action.VIEW");
							Uri uri = Uri.parse(getItem(position).getVideoUrl());
							intent.setData(uri);
							context.startActivity(intent);
						}
						break;
					case R.id.forward_image:
						if(getItem(position).getSource().getTweetImage() != null &&
					       getItem(position).getSource().getTweetImage().length() > 0) ((MainPageFragmentCallback) context).setupPictureDialog(getItem(position).getSource().getTweetImage().getString(0), "/2000");
						if(getItem(position).getSource().getVideoImage() != null){
							Intent intent = new Intent();
							intent.setAction("android.intent.action.VIEW");
							Uri uri = Uri.parse(getItem(position).getSource().getVideoUrl());
							intent.setData(uri);
							context.startActivity(intent);
						}
						break;
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		};
		holder.headImage.setOnClickListener(tweetListClickListener);
		holder.tweetImage.setOnClickListener(tweetListClickListener);
	    holder.forwardImage.setOnClickListener(tweetListClickListener);
		
		ImageLoader.getInstance(context).displayImage(tweetBean.getHead()+"/50", holder.headImage, 6);
		holder.title.setText(tweetBean.getNick());
		if(tweetBean.getIsVip() == 1){
			holder.isVip.setVisibility(View.VISIBLE);
		}else{
			holder.isVip.setVisibility(View.GONE);
		}
		holder.timestamp.setText(TimeUtil.converTime(tweetBean.getTimestamp(), 2));
		holder.from.setText(context.getString(R.string.from)+tweetBean.getFrom());
		holder.commentCount.setText(tweetBean.getMCount());
		holder.forwardCount.setText(tweetBean.getCount());
		String tmp_text = tweetBean.getText();
		if(tweetBean.getGeo() != null && tweetBean.getGeo().length() > 0) {
			holder.tweetLocationText.setVisibility(View.VISIBLE);
			holder.tweetLocationText.setText(context.getString(R.string.i_am) + tweetBean.getGeo());
		}else{
			holder.tweetLocationText.setVisibility(View.GONE);
		}
		if(tweetBean.getSource() != null && tweetBean.getText().length() == 0){
			holder.tweetContent.setText(context.getString(R.string.re_tweet));
		}else{
			ContentTransUtil.getInstance(context).displaySpannableString(tmp_text, holder.tweetContent, tweetBean.getMentionedUser());
		}
		if(tweetBean.getTweetImage() != null && !tweetBean.getTweetImage().toString().equals("[]")){
			holder.tweetImage.setVisibility(View.VISIBLE);
			holder.hasImage.setVisibility(View.VISIBLE);
			try {
				ImageLoader.getInstance(context).displayImage(tweetBean.getTweetImage().getString(0)+"/120", holder.tweetImage, 1);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}else if(tweetBean.getVideoImage() != null && !tweetBean.getVideoImage().toString().equals("null")){
			holder.tweetImage.setVisibility(View.VISIBLE);
			holder.hasImage.setVisibility(View.GONE); 
		    ImageLoader.getInstance(context).displayImage(tweetBean.getVideoImage(), holder.tweetImage, 1);
		}else{
			holder.tweetImage.setVisibility(View.GONE);
			holder.hasImage.setVisibility(View.GONE); 
		}
		
		//是否转发
		if(tweetBean.getSource() != null && !tweetBean.getSource().getNick().equals("null")){
			holder.sourceLayout.setVisibility(View.VISIBLE);
			String tmp_source_text = tweetBean.getSource().getText();
			if(tweetBean.getSource().getGeo() != null && tweetBean.getSource().getGeo().length() > 0){
				holder.forwardLocationText.setVisibility(View.VISIBLE);
				holder.forwardLocationText.setText(context.getString(R.string.i_am) + tweetBean.getSource().getGeo());
			}else{
				holder.forwardLocationText.setVisibility(View.GONE);
			}
			ContentTransUtil.getInstance(context).displaySpannableString("<font color='0x2078DC'>" + tweetBean.getSource().getNick() + ":</font>" + tmp_source_text, holder.forwardContent, tweetBean.getMentionedUser());
			if(tweetBean.getSource().getTweetImage() != null && !tweetBean.getSource().getTweetImage().toString().equals("[]")){
				holder.forwardImage.setVisibility(View.VISIBLE);
				try {
					ImageLoader.getInstance(context).displayImage(tweetBean.getSource().getTweetImage().getString(0)+"/120", holder.forwardImage, 1);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}else if(tweetBean.getSource().getVideoImage() != null && !tweetBean.getSource().getVideoImage().toString().equals("null")){
				holder.forwardImage.setVisibility(View.VISIBLE);
			    ImageLoader.getInstance(context).displayImage(tweetBean.getSource().getVideoImage(), holder.forwardImage, 1);
		    }else{
		    	holder.forwardImage.setVisibility(View.GONE);
		    }
		}else{
			holder.sourceLayout.setVisibility(View.GONE);
		}
		
		
		return convertView;
	}
	
	private static final class TweetViewHolder{
		ImageView headImage;
		TextView title;
		ImageView isVip;
		TextView timestamp;
	    TextView from;
		TextView commentCount;
		TextView forwardCount;
		TextView tweetContent;
		TextView tweetLocationText;
		ImageView tweetImage;
        TextView forwardContent;
		TextView forwardLocationText;
		ImageView forwardImage;
		ImageView hasImage;
		RelativeLayout sourceLayout;
	}

}
